home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / gmp-132.lha / gmp-1.3.2 / mpz_sizeinb.c < prev    next >
C/C++ Source or Header  |  1993-05-02  |  2KB  |  60 lines

  1. /* mpz_sizeinbase(x, base) -- return an approximation to the number of
  2.    character the integer X would have printed in base BASE.  The
  3.    approximation is never too small.
  4.  
  5. Copyright (C) 1991 Free Software Foundation, Inc.
  6.  
  7. This file is part of the GNU MP Library.
  8.  
  9. The GNU MP Library is free software; you can redistribute it and/or modify
  10. it under the terms of the GNU General Public License as published by
  11. the Free Software Foundation; either version 2, or (at your option)
  12. any later version.
  13.  
  14. The GNU MP Library is distributed in the hope that it will be useful,
  15. but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17. GNU General Public License for more details.
  18.  
  19. You should have received a copy of the GNU General Public License
  20. along with the GNU MP Library; see the file COPYING.  If not, write to
  21. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  22.  
  23. #include "gmp.h"
  24. #include "gmp-impl.h"
  25. #include "longlong.h"
  26.  
  27. size_t
  28. #ifdef __STDC__
  29. mpz_sizeinbase (const MP_INT *x, int base)
  30. #else
  31. mpz_sizeinbase (x, base)
  32.      const MP_INT *x;
  33.      int base;
  34. #endif
  35. {
  36.   mp_size size = ABS (x->size);
  37.   int lb_base, cnt;
  38.   size_t totbits;
  39.  
  40.   /* Special case for X == 0.  */
  41.   if (size == 0)
  42.     return 1;
  43.  
  44.   /* Calculate the total number of significant bits of X.  */
  45.   count_leading_zeros (cnt, x->d[size - 1]);
  46.   totbits = size * BITS_PER_MP_LIMB - cnt;
  47.  
  48.   if ((base & (base - 1)) == 0)
  49.     {
  50.       /* Special case for powers of 2, giving exact result.  */
  51.  
  52.       count_leading_zeros (lb_base, base);
  53.       lb_base = BITS_PER_MP_LIMB - lb_base - 1;
  54.  
  55.       return (totbits + lb_base - 1) / lb_base;
  56.     }
  57.   else
  58.     return (size_t) (totbits * __mp_bases[base].chars_per_bit_exactly) + 1;
  59. }
  60.